package com.kaishengit.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.google.gson.Gson;
import com.kaishengit.pojo.Contact;
import com.kaishengit.pojo.Group;
import com.kaishengit.pojo.GroupUser;
import com.kaishengit.pojo.User;
import com.kaishengit.pojo.myJson;
import com.kaishengit.service.ContactService;
import com.kaishengit.service.GroupService;
import com.kaishengit.service.UserService;
import com.opensymphony.xwork2.ActionSupport;


public class EmailAction extends ActionSupport implements ServletRequestAware {
	

	private static final long serialVersionUID = 1L;
	private String contacts;
	private List<Contact> contactlist;
	//private List<GroupUser> gulist;
	private List<Group> glist;
	private ContactService contactService;
	private GroupService groupService;
	private HttpServletRequest request;
	private User user;
	private UserService userService;
	
	//进入发送邮件页面
	public String execute(){
		System.out.println("^^^^^^^^^^^^^^^^^^");
		HttpSession session = request.getSession();
		user = (User) session.getAttribute("curr_user");
		System.out.println(user.getUsername());
		contactlist=contactService.findAll();
		glist=groupService.findAll();
		System.out.println(glist.size());
		for(Group g:glist){
			System.out.println(g.getUserSet().size());
			//System.out.println(gu.getGroup().getName());
			System.out.println("$$$$$$$$$$$$$$$$$$");
			
		}
		return "success";
	}
	
	//确定联系人后进入发送邮件页面
	public String sendemail(){
		System.out.println("^^^^^^^^^^^^^^^^^^");
		HttpSession session = request.getSession();
		user = (User) session.getAttribute("curr_user");
		System.out.println(user.getUsername());
		System.out.println(contacts);
		return "success";
	}
	//发送邮件
	public String emailtoperson(){
		Email email = new SimpleEmail(); 
		email.setHostName("smtp.163.com");
		email.setAuthentication("momoklmyta", "hong19890825"); 
		email.setCharset("UTF-8"); 
		email.setTLS(true); 
		try { 
		email.setFrom("momoklmyta@163.com"); 
		email.setSubject("commons email"); 
		email.setMsg("这是利用commons包发出的电子邮件"); 
		email.addTo("860611571@qq.com"); 
		email.send(); 
		} catch (EmailException e) { 
		  e.printStackTrace(); 
		} 
		return "success";
	}
	
	//获得json
	public String emailjson() throws IOException{
		HttpServletResponse response = ServletActionContext.getResponse();
		//List<String> createtree=new ArrayList<String>();
		List<myJson> createtree=new ArrayList<myJson>();
		glist=groupService.findAll();
		int i=0;
		for(Group g:glist){
			i=i+1;
			/*String str = null;
			str = "{id:" + i + "," + "pId:"
	                + i + "," + "name:"
	                + "'"+g.getName()+"'" +","+ "open:true" + "}";*/
			myJson mj=new myJson();
			mj.setId(Integer.toString(i));
			mj.setpId(Integer.toString(i));
			mj.setName(g.getName());
			mj.setOpen("open:true");
			createtree.add(mj);
			List<User> ulist=userService.findByGroupId(g);
			int j=0;
			for(User u:ulist){
				j=j+1;
				//String st=null;
				myJson m=new myJson();
				/*st= "{id:" + i+j+ "," + "pId:"
		                + i + "," + "name:"
		                + "'"+u.getUsername()+"'" +","+ "open:true" + "}";*/
				m.setId(Integer.toString(i)+Integer.toString(j));
				m.setpId(Integer.toString(i));
				m.setName(u.getUsername());
				m.setOpen("open:false");
				createtree.add(m);
			}
		}
		contactlist=contactService.findAll();
		int q=100;
		myJson mjs=new myJson();
		mjs.setId(Integer.toString(q));
		mjs.setpId(Integer.toString(q));
		mjs.setName("其他联系人");
		mjs.setOpen("open:true");
		createtree.add(mjs);
		int p=0;
		for(Contact con:contactlist){
			p=p+1;
			myJson mjso=new myJson();
			mjso.setId(Integer.toString(q)+Integer.toString(p));
			mjso.setpId(Integer.toString(100));
			mjso.setName(con.getName());
			mjso.setOpen("open:false");
			createtree.add(mjso);
			q=q+1;
		}
		//printWriter(response, JSONArray.fromObject(createtree).toString());
		response.setContentType("application/json;charset=UTF-8");
		Gson gson = new Gson();
		PrintWriter out = response.getWriter();
		out.print(gson.toJson(createtree));
		out.flush();
		out.close();
		
		return null;
	}

	
	
	
	
	//set get
	public String getContacts() {
		return contacts;
	}

	public void setContacts(String contacts) {
		this.contacts = contacts;
	}

	public List<Contact> getContactlist() {
		return contactlist;
	}

	public void setContactlist(List<Contact> contactlist) {
		this.contactlist = contactlist;
	}
	/*public List<GroupUser> getGulist() {
		return gulist;
	}

	public void setGulist(List<GroupUser> gulist) {
		this.gulist = gulist;
	}*/
	public List<Group> getGlist() {
		return glist;
	}

	public void setGlist(List<Group> glist) {
		this.glist = glist;
	}
	
	
	
	
	
	
	
	//set

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setContactService(ContactService contactService) {
		this.contactService = contactService;
	}

	

	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
}
